package com.hopu.blb_erp.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopu.blb_erp.entity.Department;
import com.hopu.blb_erp.entity.Order2;
import com.hopu.blb_erp.mapper.DepartmentMapper;
import com.hopu.blb_erp.service.DepartmentService;
import com.hopu.blb_erp.service.IDepartmentService;
import com.hopu.blb_erp.service.IOrder2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author A-Zhang
 * @since 2021-11-17
 */
@Api
@Controller
@RestController
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private IOrder2Service order2Service;


    @ApiOperation("查询订单中商品的生产部门信息")
    @GetMapping("/department/{id}")//传入的是前端选择的订单id
    public ResponseEntity<Department> findOrdersToHandover(@PathVariable("id") String id ){
        //订单中对应的部门信息
        Department department = departmentService.findById(order2Service.getById(id).getProductLineId());
        return ResponseEntity.ok(department);
    }

    @ApiOperation("根据id查询部门")
    @GetMapping("/department/de/{id}")
    public ResponseEntity<Department> findId(@PathVariable("id") Integer id){
        Department department = departmentService.findById(id);
        return ResponseEntity.ok(department);
    }


    @ApiOperation("添加部门")
    @PostMapping("/insertDepart")
    public ResponseEntity<String> add(@RequestBody Department department){
        System.out.println(department);
        departmentService.addDepart(department);
        return ResponseEntity.ok("ok");
    }



    @ApiOperation("删除部门")
    @DeleteMapping("/department/{id}")
    public ResponseEntity<String> delete(@PathVariable("id") Integer id){
        departmentService.removeDepart(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("修改部门")
    @PutMapping("/department")
    public ResponseEntity<String> update(@RequestBody Department department){
        departmentService.modifyDepart(department);
        return ResponseEntity.ok("ok");
    }


    @GetMapping("departments")
    public ResponseEntity<List<Department>> findAll(){
        List<Department> departments = departmentService.findTree();

        return ResponseEntity.ok(departments);
    }
}
